//职员管理

import React, {Component} from 'react';
import app from '../app';
import {Form, Input, Button, Table, Modal, Row, Upload, Icon,DatePicker} from 'antd';
import {project,employee } from '../value';
import '../css/other.css';
import moment from 'moment';
import {config} from "../config";
import {AddRecharges,EditAccounts} from '../components/operateManage';

import {EditorState, convertToRaw, ContentState} from 'draft-js';
import {Editor} from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import draftToHtml from 'draftjs-to-html';
import htmlToDraft from 'html-to-draftjs';

const FormItem = Form.Item;
const confirm = Modal.confirm;

class RechargeSetting extends Component {
    constructor(props) {
        super(props);
        this.state = {
            searchData: {},
            pagination: {current: 1, pageSize: 10, total: 0},
            data: [
                {
                    order_number:1,
                    admin_number:122,
                    admin_username:"cbl",
                    admin_account:"acctount123",
                    created_at:'2018-12-12',
                    admin_status:1
                }
            ],
            currentPageSize: 0,
            pageCount: 0,
            imgUrl: '',
        }
    }

    columns = [
        {
            title: '序号',
            dataIndex: 'order_number'
        },
        {
            title: '充值金额',
            dataIndex: 'admin_number'
            // render:project_picture=>{
            //     return(
            //         <img className={"projectImg"} src={config.imgServerUrl+project_picture} />
            //     )
            // }
        },
        {
            title: '送金额',
            dataIndex: 'admin_username'
        },
        {
            title: '已充值次数',
            dataIndex: 'admin_account'
        },
        {
            title: '已充值总额',
            dataIndex: 'admin_account'
        },
        {
            title: '创建时间',
            dataIndex: 'created_at',
            render:created_at=>{
                return(
                    <span>{moment(created_at).format('YYYY-MM-DD HH:mm:ss')}</span>
                )
            }
        },
        {
            title: '操作',
            dataIndex: this.columns,
            render: data => {
                return (
                    <div>
                        <Button style={{marginLeft:'10px'}} type="danger" onClick={() => this.handleDeleteProject(data.project_id)}>删除</Button>
                    </div>
                )
            }
        },
    ];

    render() {
        return (
            <div style={{paddingBottom: 25}}>
                <div className="pageTitle">
                    充值设置
                </div>
                <SearchLists refresh={()=>this.handleRefreshAfterCreate()} clickSearch={(values) => this.handleGetValue(values)}/>
                <Row style={{marginTop: '20px'}}>
                    <Table
                        columns={this.columns}
                        rowKey={(record, index) => index}
                        dataSource={this.state.data}
                        loading={this.state.loading}
                        pagination={false}
                        onChange={this.handleTableChange}
                    />
                </Row>
            </div>
        )
    }

    /**
     * 初始化
     */
    componentDidMount() {
        //this.searchProject({});
    }

    /**
     * 获取搜索条件
     */
    handleGetValue(value) {
        const searchData = {project_name:value};
        this.setState({searchData});
        const param = {
            ...searchData
        };
        this.searchProject(param);
    }

    /**
     * 处理创建后刷新事件
     */
    handleRefreshAfterCreate() {
        const param = {};
        this.searchProject(param);
    }

    /**
     * 处理删除后刷新事件
     */
    handleRefreshAfterDelete() {
        const {searchData} = this.state;
        const param = {
            ...searchData
        };
        this.searchProject(param);
    }

    /**
     * 处理更新后刷新事件
     */
    handleRefreshAfterUpdate() {
        const {searchData} = this.state;
        const param = {
            ...searchData
        };
        this.searchProject(param);
    }

    /**
     * 处理页码、显示数量变化事件
     * @param pagination
     * @param filters
     * @param sorter
     */
    handleTableChange = (pagination, filters, sorter) => {
        const current = pagination.current;
        const size = pagination.pageSize;
        const pagination1 = this.state.pagination;
        pagination1.current = current;
        pagination1.pageSize = size;
        this.setState({pagination: pagination1});
    };

    /**
     * 查找项目列表
     * @param param
     */
    searchProject(param) {
        let _this = this;
        _this.setState({loading: true});
        app.post(project.SEARCH_PROJECT, param).then(result => {
            if (result.code === 1) {
                const data=result.data;
                for (let i=0;i<data.length;i++){
                    const content=data[i].project_content;
                    if(content.length>10){
                        data[i].project_content=content.substr(0,10)+'...';
                    }
                }
                const pagination={total:data.length,current:1,pageSize:10};
                this.setState({pagination, data: data, loading: false });
            } else {
                app.alert(result.msg, 'success', 2);
                const pagination = {total: 0, current: 0, pageSize: 0};
                this.setState({pagination, data: [], loading: false});
            }
        });
    }

    /**
     * 删除项目
     * @param id
     */
    handleDeleteProject(id) {
        let _this = this;
        confirm({
            content: '您确定要删除该项目吗？',
            onOk() {
                app.post(project.DELETE_PROJECT, {project_id: id}).then((req) => {
                    if (req.code == '1') {
                        app.alert("删除成功", 'success', 2);
                        _this.handleRefreshAfterDelete();
                    } else {
                        app.alert(req.msg, 'error', 2);
                    }
                })
            },
            onCancel() {
            },
        });
    }
}


class SearchList extends Component {
    constructor(props) {
        super(props);
        this.state = {
            project_name:''
        }
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        return (
            <div style={{display: 'flex', marginTop: 10, marginBottom: 5}}>
                <div style={{display: 'flex', width: '250px', height: '28px'}}>
                    <div style={{width: 50, height: 28, lineHeight: '28px', color: '#000000'}}>时间：</div>
                    {/*<Input onPressEnter={() => this.handleSearchComplain1()} onChange={(e) => this.handleOnChange(e)}*/}
                           {/*type="text" placeholder="用户名"/>*/}
                    <DatePicker placeholder={'请选择时间'} style={{width:'200px'}}/>
                </div>
                <div style={{display: 'flex', width: '120px', height: '28px', marginLeft: 20}}>
                    <Button type="primary" onClick={() => this.handleSearchComplain1()}>搜索</Button>
                    <AddRecharges refresh={() => this.handleRefreshAfterCreate()}></AddRecharges>
                </div>
            </div>
        )
    };

    componentDidMount() {
    };

    /**
     * 处理账号输入框变化事件
     * @param e
     */
    handleOnChange(e) {
        this.setState({project_name: e.target.value});
    }

    /**
     * 获取搜索条件
     */
    handleSearchComplain1() {
        let {project_name} = this.state;
        this.props.clickSearch(project_name);
    };

    handleRefreshAfterCreate(){
        this.props.refresh();
    }
}

const SearchLists = Form.create()(SearchList);
const RechargeSettings = Form.create()(RechargeSetting);

export default RechargeSettings


